package com.f2prateek.rx.preferences2.sample.slice;

import com.f2prateek.rx.preferences2.Preference;
import com.f2prateek.rx.preferences2.RxSharedPreferences;
import com.f2prateek.rx.preferences2.sample.ResourceTable;
import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.RadioButton;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

public class MainAbilitySlice extends AbilitySlice {
    private final CompositeDisposable disposables = new CompositeDisposable();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //创建RxSharedPreferences
        DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
        Preferences preferences = databaseHelper.getPreferences("app_preferences");
        RxSharedPreferences rxPreferences = RxSharedPreferences.create(preferences);

        //获取值
        Preference<Boolean> isMalePreference = rxPreferences.getBoolean("isMale");
        Preference<String> namePreference = rxPreferences.getString("name");

        //设置输入框变化监听
        TextField nameInput = (TextField) findComponentById(ResourceTable.Id_name_input);
        nameInput.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String text, int start, int before, int count) {
                namePreference.set(text);
            }
        });
        //设置单选框选中监听
        RadioContainer sexRadioContainer = (RadioContainer) findComponentById(ResourceTable.Id_sex_radio_container);
        sexRadioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int index) {
                Boolean isMale = null;
                RadioButton button = (RadioButton) radioContainer.getComponentAt(index);
                int buttonId = button.getId();
                if (ResourceTable.Id_male == buttonId) {
                    isMale = true;
                } else if (ResourceTable.Id_female == buttonId) {
                    isMale = false;
                }
                if (isMale == null) {
                    return;
                }
                isMalePreference.set(isMale);
            }
        });

        //监听RxSharedPreferences值变化
        disposables.add(isMalePreference.asObservable().subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean isMale) throws Exception {
                toast("isMale => " + isMale);
            }
        }));
        disposables.add(namePreference.asObservable().subscribe(new Consumer<String>() {
            @Override
            public void accept(String name) throws Exception {
                toast("name => " + name);
            }
        }));

        //回显值
        nameInput.setText(namePreference.get());
        sexRadioContainer.mark(isMalePreference.get() ? ResourceTable.Id_male : ResourceTable.Id_female);
    }

    @Override
    protected void onStop() {
        super.onStop();
        disposables.dispose();
    }

    private void toast(String msg) {
        ToastDialog toast = new ToastDialog(getContext());
        toast.setText(msg);
        toast.setDuration(1000);
        toast.show();
    }
}